import type { ZRTC } from 'zrtc'

export const useZRTC = defineStore('zrtc', () => {
  const rtc = inject<ZRTC>('rtc')

  const producer = ref<Record<string, any>>()

  const peers = ref<Record<string, RTC.Peer>>()
  const speaker = ref<Record<string, number>>({})

  if (rtc) {
    rtc.on('activeSpeaker', ({ volume, peerId }: RTC.ActiveSpeak) => {
      const vol = volume ? Math.round(Math.pow(10, volume / 85) * 10) : 0
      console.log('activeSpeaker', peerId, volume, vol)
      if (peerId) speaker.value[peerId] = vol
    })
  }

  return {
    rtc,
    peers,
    producer,
    speaker
  }
})
